#include "gameview.h"
#include "game/gamescene.h"
#include <QFocusEvent>
#include "view/pauseview.h"
#include "toolkit.h"
#include "view/hintview.h"
#include "game/teachgamescene.h"

gameview::gameview(QWidget *parent) :
    QGraphicsView(parent),
    m_gamescene(NULL),
    m_tgscene(NULL),
    m_pauseview(NULL)
{
    setFrameShape(QFrame::NoFrame);

    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    if(ToolKit::m_curlevel == 0)
    {
        m_tgscene = new TeachGameScene(this);
        setScene(m_tgscene);
        connect(m_tgscene,SIGNAL(GameOver(bool)),this,SLOT(GameOver(bool)));
        connect(m_tgscene,SIGNAL(showPause()),this,SLOT(showPause()));
    }
    else
    {
        m_gamescene = new gamescene(this);
        setScene(m_gamescene);
        connect(m_gamescene,SIGNAL(GameOver(bool)),this,SLOT(GameOver(bool)));
        connect(m_gamescene,SIGNAL(showPause()),this,SLOT(showPause()));
    }

    setGeometry(0.0,0.0,640.0,360.0);
}

gameview::~gameview()
{
    if(m_gamescene != NULL)
    {
        delete m_gamescene;
        m_gamescene = NULL;
        ToolKit::m_gamescene = NULL;
    }

    if(m_tgscene != NULL)
    {
        delete m_tgscene;
        m_tgscene = NULL;
        ToolKit::m_teachGameScene = NULL;
    }
}

void gameview::GameOver(bool bclear)
{
    emit Over(bclear);
}

void gameview::GameContinue()
{
    deletePauseView();
    if(ToolKit::m_curlevel != 0)
        m_gamescene->GameContinue();
    else
        m_tgscene->GameContinue();
}

void gameview::GameRestart()
{
    deletePauseView();
    if(ToolKit::m_curlevel != 0)
        m_gamescene->init();
    else
        m_tgscene->init();
}

void gameview::showPause()
{
   createPauseView();
}



void gameview::createPauseView()
{
    if(m_pauseview == NULL)
    {
        m_pauseview = new PauseView(this);
        m_pauseview->showFullScreen();
        connect(m_pauseview,SIGNAL(gamecontinue()),this,SLOT(GameContinue()));
        connect(m_pauseview,SIGNAL(gamerestart()),this,SLOT(GameRestart()));
        connect(m_pauseview,SIGNAL(gameback()),this,SLOT(GameBack()));
    }
}

void gameview::deletePauseView()
{
    if(m_pauseview != NULL)
    {
        m_pauseview->setVisible(false);
        delete m_pauseview;
        m_pauseview = NULL;
    }
}

void gameview::GameBack()
{
    deletePauseView();

    emit Over(false);
}
